package com.sws.controller;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.sws.Constant;
import com.sws.model.bean.C2SPageBean;
import com.sws.service.HomeService;

@Controller
public class HomeController {

	private Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@Autowired
	private HomeService service;
	
	@ResponseBody
	@RequestMapping("/h/getPushBanner")
	public String getPushBanner() {
		logger.info("====> 获取轮播");
		JSONObject result = new JSONObject();
		try {
			result.put("status", Constant.INSTANSE.OK);
			result.put("data", service.getPushBanner());
			result.put("msg", "获取轮播成功！");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/h/getPushNews")
	public String getPushNews(C2SPageBean bean) {
		logger.info("====> 获取资讯");
		JSONObject result = new JSONObject();
		try {
			result.put("status", Constant.INSTANSE.OK);
			result.put("data", service.getPushNews(bean));
			result.put("msg", "获取资讯成功！");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/h/getNews")
	public String getNews(Integer id) {
		logger.info("====> 获取资讯内容");
		JSONObject result = new JSONObject();
		try {
			result.put("status", Constant.INSTANSE.OK);
			result.put("data", service.getNews(id));
			result.put("msg", "获取资讯内容成功！");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/h/getBaseDatas")
	public String getBaseData() {
		logger.info("====> 获取基础数据");
		JSONObject result = new JSONObject();
		try {
			result.put("status", Constant.INSTANSE.OK);
			result.put("data", service.getBaseData());
			result.put("msg", "获取基础数据成功！");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
}
